package com.hontech.desktoppastacooker.utils

data class ByteSlice (var data: ByteArray = byteArrayOf(),
                 var index: Int = 0,
                 var len: Int = data.size - index) {

    companion object {

        fun new(len: Int): ByteSlice {
            return ByteSlice(ByteArray(len), 0, len)
        }
    }

    fun take(): Byte {
        val value = data[index]
        index ++
        len --
        return value
    }

    override fun toString(): String {
        return data.toHex(index, len)
    }

    fun toUtf8(): String {
        return String(data, index, len)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as ByteSlice

        if (!data.contentEquals(other.data)) return false
        if (index != other.index) return false
        if (len != other.len) return false

        return true
    }

    override fun hashCode(): Int {
        var result = data.contentHashCode()
        result = 31 * result + index
        result = 31 * result + len
        return result
    }
}


